import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import processing.core.PApplet;
import processing.core.PImage;

public class Pulpo {

	public int posx = 0;
	public int posy = 0;

	float inc = 0.0f;

	float a1 = 0.012f;
	float a2 = 0.001f;
	float a3 = 0.002f;
	float a4 = 0.003f;
	float a5 = 0.0025f;
	float a6 = 0.0034f;
	float a7 = 0.014f;
	float a8 = 0.0028f;
	float rot1, rot2, rot3, rot4, rot5, rot6, rot7, rot8;

	public static int MOVE_THRESHOLD = 20;
	int countMovement = 0;

	float countKeyPressed = 0;
	int timeAttacking = 25;
	boolean atack = false;
	boolean upping = false;

	public List<PulpoPoint> mask = null;
	public Point[] atackMask = null;
	private float attackRotation = PApplet.radians(90);

	public static int ARMS_NUM = 8;
	public int countPoints = ARMS_NUM * 30;

	int[] activeArms = new int[ARMS_NUM];

	float currentRotation = 0;
	
	private boolean left = true;

	public void setup(PApplet pApplet) {
		mask = new ArrayList<PulpoPoint>();
		reset();
	}

	private void reset() {
		activeArms[0] = 24;
		activeArms[1] = 21;
		activeArms[2] = 18;
		activeArms[3] = 17;
		activeArms[4] = 19;
		activeArms[5] = 20;
		activeArms[6] = 24;

	}
	
	public void attackToLeft(boolean toLeft){
		left = false;
	}

	public void rotate(float f, PApplet applet) {
		applet.rotate(f);
		currentRotation += f;
	}

	public void draw(PApplet applet) {

		mask = new ArrayList<PulpoPoint>();

		applet.pushMatrix();

		float incTemp = applet.random(0.003F, 0.015F);

		inc += incTemp;

		float angle1 = PApplet.sin(inc) / 10.0f + PApplet.sin(inc * 4.5f)
				/ 70.0f;
		float angle2 = PApplet.cos(inc) / 12.0f + PApplet.cos(inc * 4.5f)
				/ 80.0f;

		applet.stroke(0, 204);
		applet.translate(posx, posy);
		tail(1, 0, activeArms[0], angle1 * 4, 255, applet, true);
		rotate(PApplet.sin(rot1) + PApplet.QUARTER_PI, applet);
		tail(2, 0, activeArms[1], angle1 * 2.7f, 255, applet, true);
		rotate(PApplet.cos(rot2) + PApplet.QUARTER_PI, applet);
		tail(3, 0, activeArms[2], angle2 * 5.5f, 255, applet, true);
		rotate(rot3 + PApplet.QUARTER_PI, applet);
		tail(4, 0, activeArms[3], angle1 * 3, 255, applet, true);
		rotate(PApplet.sin(rot4 + PApplet.QUARTER_PI), applet);
		tail(5, 0, activeArms[4], angle2 * 4.2f, 255, applet, true);
		rotate(PApplet.atan(rot5) + PApplet.QUARTER_PI, applet);
		tail(6, 0, activeArms[5], angle1 * 2.4f, 255, applet, true);
		rotate(PApplet.sin(rot6) + PApplet.QUARTER_PI, applet);
		tail(7, 0, activeArms[6], angle2 * 3, 255, applet, true);
		rotate(PApplet.cos(rot7) + PApplet.QUARTER_PI, applet);

		float angle = angle1 * 3.2f;
		int cStroke = 255;

		if (atack) {
			if (upping) {
				countKeyPressed++;
				angle /= countKeyPressed;
				cStroke = applet.color(255, 0, 0);
				atackMask = new Point[27];
				if (countKeyPressed > timeAttacking) {
					upping = false;
				}
			} else {
				if (countKeyPressed > 1) {
					countKeyPressed--;
					angle /= countKeyPressed;
					cStroke = applet.color(255, 0, 0);
				} else {
					atack = false;
				}
			}
		}

		applet.popMatrix();
		
		applet.pushMatrix();
		applet.translate(posx, posy);
		if(left)
			applet.rotate(attackRotation);
		else
			applet.rotate(-attackRotation);
		
		tail(8, 0, 27, angle, cStroke, applet, !atack);
		applet.popMatrix();
		
		
		rot1 += a1;
		rot2 += a2;
		rot3 += a3;
		rot4 += a4;
		rot5 += a5;
		rot6 += a6;
		rot7 += a7;
		rot8 += a8;

	}

	public void atack() {
		atack = true;
		upping = true;
		countKeyPressed = 0;
	}

	public void wounded(int arm, int falange) {

		// System.out.println("arm: "+arm+" falange:"+falange);
		activeArms[arm - 1] = activeArms[arm - 1] - falange;
	}

	public void restart() {
		reset();
	}

	public void tail(int tailNum, int x, int units, float angle,
			int strokeColor, PApplet pApplet, boolean masked) {

		float scaleValue = 3;

		pApplet.pushMatrix();
		pApplet.translate(x, 0);

		units *= 2 / (float) 3;

		for (int i = units; i > 0; i--) {

			pApplet.strokeWeight(PApplet.sqrt(i / 6));
			pApplet.line(-i, 0, i, 0);
			pApplet.fill(255);
			pApplet.ellipse(-i, 0, 2 * scaleValue, 2 * scaleValue);
			pApplet.ellipse(i, 0, 2 * scaleValue, 2 * scaleValue);
			pApplet.stroke(strokeColor, 76);
			pApplet.strokeWeight(i / 1.7f);
			pApplet.fill(111, 54, 185);

			float xScreen = pApplet.screenX(0, 0);
			float yScreen = pApplet.screenY(0, 0);

			if (xScreen > 0 && yScreen > 0 && xScreen < pApplet.width
					&& yScreen < pApplet.height) {
				if (masked) {
					mask.add(new PulpoPoint((int) xScreen, (int) yScreen,
							tailNum, i));
				} else if (atackMask != null) {
					atackMask[i] = new Point((int) xScreen, (int) yScreen);
				}
			}

			pApplet.ellipse(0, 0, PApplet.sqrt(i * scaleValue), PApplet.sqrt(i
					* scaleValue));
			pApplet.line(0, 0, 0, -8);
			pApplet.translate(0, -10 * (scaleValue - 1));
			pApplet.rotate(angle);
		}

		pApplet.popMatrix();
	}
}
